<?php
!defined('P_W') && exit('Forbidden');
/**
 * 经纪人组服务类
 */
class PW_BrokerGroupService {
	var $_brokerGroupDB;
	
	function __construct() {
		$this->_brokerGroupDB = $this->_getBrokerGroupDB();
	}
	
	function PW_BrokerGroupService() {
		$this->__construct();
	}

	/**
	 * 获取所有经纪人组信息
	 */
	function getBrokerGroups() {
		return $this->_brokerGroupDB->findAll();
	}
	/**
	 * 获取一个经纪人组信息
	 * @param $groupid	经纪人组id
	 */
	function getBrokerGroup($groupid) {
		$groupid = (int) $groupid;
		
		$brokerGroupInfo = $this->_brokerGroupDB->get($groupid);
		if (!$brokerGroupInfo) return array();
		
		$brokerGroupRight = $this->getBrokerGroupRight($groupid);
		$brokerGroupInfo['right'] = $brokerGroupRight;
		
		return $brokerGroupInfo;
	}
	/**
	 * 获取经纪人用户组的权限
	 * @param $groupid
	 */
	function getBrokerGroupRight($groupid) {
		$groupid = (int) $groupid;
		if (!$groupid) return array();
		
		$rightFile = $this->_getGroupRightFile($groupid);
		if (!is_file($rightFile)) {
			$this->_updateRightFile($groupid);
		}
		return include $rightFile;
	}
	/**
	 * 修改经纪人组权限
	 * @param $groupid	经纪人组id
	 * @param $rights	经纪人组权限
	 */
	function updateBrokerGroupRight($groupid,$rights) {
		$groupid = (int) $groupid;
		if (!S::isArray($rights)) return false;
		
		$groupRightDB = $this->_getBrokerRightDB();
		foreach ($rights as $key=>$value) {
			$groupRightDB->update($groupid,$key,$value);
		}
		
		$this->_updateRightFile($groupid);
	}
	/**
	 * 修改经纪人用户组的名称
	 * @param $groupid	经纪人用户组id
	 * @param $name		经纪人用户组名称
	 */
	function updateBrokerGroupName($groupid,$name) {
		$groupid = (int) $groupid;
		
		return $this->_brokerGroupDB->update(array('name'=>$name),$groupid);
	}
	/**
	 * 批量修改经纪人用户组名称
	 * @param $groups
	 */
	function updateBrokerGroups($groups) {
		if (!S::isArray($groups)) return false;
		
		foreach ($groups as $group) {
			if (!$group['groupid']) continue;
			$this->updateBrokerGroupName($group['groupid'],$group['name']);
		}
	}
	/**
	 * 删除一个经纪人组
	 * @param $groupid	经纪人组id
	 */
	function deleteBrokerGroup($groupid) {
		$groupid = (int) $groupid;
		$brokerGroupInfo = $this->getBrokerGroup($groupid);
		if (!$brokerGroupInfo) return false;
		if ($brokerGroupInfo['usernum']) return false;
		
		$this->_brokerGroupDB->delete($groupid);
		return true;
	}
	/**
	 * 添加一个经纪人组
	 * @param $name	经济人组名称
	 */
	function addBrokerGroup($name) {
		$groupid = $this->_brokerGroupDB->insert(array('name'=>$name,'usernum'=>0));
		if (!$groupid) return false;
		
		$this->updateBrokerGroupRight($groupid,array('ifsale'=>1,'salenum'=>10,'ifhire'=>1,'hirenum'=>10));
	}
	/**
	 * 批量添加用户组
	 * @param $names	用户组名称
	 */
	function addBrokerGroups($names) {
		if (!S::isArray($names)) return false;
		foreach ($names as $key=>$value) {
			$this->addBrokerGroup($value['name']);
		}
		return true;
	}
	/**
	 * 向一个组添加经纪人
	 * @param $groupid
	 * @param $num
	 */
	function addBrokerUserNum($groupid,$num=1) {
		$this->_brokerGroupDB->addUsernum($groupid,$num);
	}
	/**
	 * 经纪人组删除用户
	 * @param $groupid
	 * @param $num
	 */
	function reduceBrokerUsernum($groupid,$num=1) {
		$this->_brokerGroupDB->reduceUsernum($groupid,$num);
	}
	
	
	/**
	 * 修改权限缓存文件
	 * @param $groupid	经纪人组id
	 */
	function _updateRightFile($groupid) {
		$rights = $this->_getBrokerGroupRight($groupid);
		$rightFile = $this->_getGroupRightFile($groupid);
		$this->_writeRightFile($rightFile,$rights);
	}
	
	/**
	 * 从数据库读取用户组权限
	 * @param $groupid	经纪人组id
	 */
	function _getBrokerGroupRight($groupid) {
		$groupid = (int) $groupid;
		
		$groupRightDB = $this->_getBrokerRightDB();
		$groupDB = $this->_getBrokerGroupDB();
		$rights = $groupRightDB->getRightsByGroupid($groupid);
		$group = $groupDB->get($groupid);
		$temp['name'] = $group['name'];
		$temp['groupid'] = $group['groupid'];
		foreach ($rights as $value) {
			$temp[$value['rightkey']] = $value['rightvalue'];
		}
		return $temp;
	}
	/**
	 * 重写权限缓存文件
	 * @param $rightFile	缓存文件路经
	 * @param $rights		权限
	 */
	function _writeRightFile($rightFile,$rights) {
		$temp = "<?php\r\n!defined('P_W') && exit('Forbidden');\r\n";
		$temp .= 'return '.pw_var_export($rights).';';
		writeover($rightFile,$temp);
	}
	
	function _getGroupRightFile($groupid) {
		$groupid = (int) $groupid;
		$basePath = D_P.'data/groupdb/';
		$rightFile = 'brokergroup_'.$groupid.'.php';

		return $basePath.$rightFile;
	}
	
	/**
	 * 获取经纪人组dao
	 * @return PW_House_BrokerGroupDB
	 */
	function _getBrokerGroupDB() {
		return House::loadDB('house_brokergroup');
	}
	
	/**
	 * 获取经纪人组dao
	 * @return PW_House_BrokerRightDB
	 */
	function _getBrokerRightDB() {
		return House::loadDB('house_brokerright');
	}
}